/**
 * ****************************** ICE *******************************
 * ========================================================================
 * Project:  basicsipcall
 * Package: org.ice4apps.talk.callManager.Features.Routing 
 * File:  ROrigInitiateInvite.java
 * Author: biju
 * Date: Feb 15, 2010
 * Description:
 * ========================================================================
 */
package org.ice4apps.talk.callManager.Features.Preprocess;

import java.util.Timer;

import javax.servlet.ServletException;
import javax.servlet.sip.Address;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipSession;
import javax.servlet.sip.URI;


import org.ice4apps.util.IceLog;
import org.ice4apps.talk.callManager.IceTalkFeature;
import org.ice4apps.talk.callManager.UserSession;
import org.ice4apps.talk.entity.UserSessionType;
import org.ice4apps.talk.entity.MessageType;
import org.ice4apps.talk.entity.UserSessionMessage;

/**
 * @author biju
 * This worker object will be invoked when the Sate Machine receive 
 * Message : SIP INVITE 
 * Type : Originating 
 * State : Initiate 
 */
public class PreOrigInitiateInvite extends IceTalkFeature {
	

	/**
	 * @throws ServletException
	 */
	public PreOrigInitiateInvite(UserSession userSession) throws ServletException {
		super();
		this.userSession = userSession;
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see org.ice4apps.talk.callManager.IceTalkFeature#processMessage(org.ice4apps.talk.entity.UserSessionMessage)
	 */
	public void processMessage(UserSessionMessage msg) {

		SipServletRequest origRequest = (SipServletRequest)msg.getMessage();
		String dialedDigits = origRequest.getTo().getValue();
		
		IceLog.info(this.getClass(), "processMessage dialedDigits : " + dialedDigits);
		
		// TODO Do the digit Analysis and get the destination URI
		
		// Create Termination Servlet and create the INVITE and post it to the Session Manager
		try {

			//TODO: Read this from the DB
			String requestURIString = "sip:3061974011@127.0.0.1:3210";  
	 
			URI to = requestURIString==null? null : sipFactory.createAddress(requestURIString).getURI();       
			SipServletRequest request = sipFactory.createRequest
			            (session, "INVITE", origRequest.getFrom().getURI(), to);
			
			IceLog.info(this.getClass(), "Original Request URI : " + origRequest.getRequestURI().toString());
			//TODO: Read this from the DB
			//String requestURIString = "sip:3061974011@127.0.0.1:3210";

			URI requestURI = sipFactory.createURI(requestURIString);
			request.setRequestURI(requestURI);

			IceLog.info(this.getClass(), "New Request URI : " + request.getRequestURI().toString());
			
			request.getSession().setAttribute("USER", userSession.getUserSessionManager().getUser());
			
			Object sdpObj = origRequest.getContent();
			byte[] sdpBytes = (byte[]) sdpObj;
			String sdp = new String(sdpBytes);

			request.setContent(sdp, "application/sdp");
			request.setContentLength(sdp.length());
			
			SipSession sipSession = request.getSession();
			//TODO Set all the parameters from the Original request to this one
			
			userSession.setTermMsg(new UserSessionMessage (MessageType.SIP_INVITE,
					UserSessionType.PROXY,
					sipSession.getId(),
					msg.getUser(),
					request));

			userSession.setUserSessionState(UserSession.UserSessionState.CONNECTED);
			//request.send();
			userSession.setPostMessage(true);
		} catch (Exception e) {
			// TODO : Add proper error
			//log.error("Error", e);
		}

		return;
	}

	private UserSession userSession;
}
